package service.impl.v2;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.AwardVOMapper;
import dao.v2.ConfigVOMapper;
import dao.v2.MemberVOMapper;
import dao.v2.MoneyVOMapper;
import model.BankVO;
import model.CashRechargeVO;
import model.LogBuyCashVO;
import model.LogCashDrawVO;
import model.LogEarnVO;
import model.MemberVO;
import model.OrderPayVO;
import service.v2.PushInterService;
import service.v2.PushService;
import util.NumberUtil;

@Service
public class MoneyServiceImpl implements service.v2.MoneyService {
	@Autowired
	private MoneyVOMapper dao;
	@Autowired
	private AwardVOMapper awardDao;
	@Autowired
	private MemberVOMapper memberDao;
	@Autowired
	private ConfigVOMapper  configDao;
	@Autowired
	private PushService  pushservice;
	@Autowired
	private PushInterService  pushinterservice;


	public String getUnSettledMoney(String member_id) {
		return dao.getUnSettledMoney(member_id) ;
	}
	public double calcFee(String type, String member_id, String money) {
		MemberVO memberVO = memberDao.getAllInfoById(member_id);
		BankVO bankVO = null;
		if("1".equals(type)){
			bankVO = dao.getDrawBank(memberVO.getAlipay_id());
		}else if("2".equals(type)){
			bankVO = dao.getDrawBank(memberVO.getBank_id());
		}
		return calcFeeDetail(money, bankVO);
	}
	public double calcFeeDetail(String money, BankVO bankVO) {
		double fee = 0;
		fee = Double.parseDouble(money)*Double.parseDouble(bankVO.getFee());
		if("Y".equals(bankVO.getUse_max()) && fee > Double.parseDouble(bankVO.getMax())){
			fee = Double.parseDouble(bankVO.getMax());
		} 
		if("Y".equals(bankVO.getUse_min()) && fee < Double.parseDouble(bankVO.getMin())){
			fee = Double.parseDouble(bankVO.getMin());
		}
		return NumberUtil.formatDoubleUp(fee, 2);
	}
	public int insertDraw(LogCashDrawVO drawVO,String member_id, String type, String free_cash) {
		//减掉余额
		int re = dao.subCash(member_id,free_cash);
		if(re > 0){
			//提现订单
			dao.insertDraw( drawVO,member_id,  type,  free_cash);
			//检查最大最小值 如果设置 修改手续费
			LogCashDrawVO draw = dao.getLogCashDrawById(drawVO.getId());
			BankVO bankVO  = dao.getDrawBank(draw.getBank_id());
			if("Y".equals(bankVO.getUse_max()) && Double.parseDouble(draw.getFee()) > Double.parseDouble(bankVO.getMax())){
				dao.updateDrawFee(drawVO.getId(), bankVO.getMax());
			} 
			if("Y".equals(bankVO.getUse_min()) && Double.parseDouble(draw.getFee()) < Double.parseDouble(bankVO.getMin())){
				dao.updateDrawFee(drawVO.getId(), bankVO.getMin());
			}
			draw =  dao.getLogCashDrawById(drawVO.getId());
			//提现日志生成
			dao.insertLogDraw(draw);
			//提现订单关联日志
			dao.updateDrawConn(draw);
		}
		return re;
	}
	
	@Override
	public int insertDrawInter(LogCashDrawVO drawVO, String member_id, String type, String free_cash) {
		//减掉余额
		int re = dao.subCashEn(member_id,free_cash);
		if(re > 0){
			//提现订单
			dao.insertDrawInter( drawVO,member_id,  type,  free_cash);
			//检查最大最小值 如果设置 修改手续费
			LogCashDrawVO draw = dao.getLogCashDrawById(drawVO.getId());
			BankVO bankVO  = dao.getDrawBank(draw.getBank_id());
			if("Y".equals(bankVO.getUse_max()) && Double.parseDouble(draw.getFee()) > Double.parseDouble(bankVO.getMax())){
				dao.updateDrawFee(drawVO.getId(), bankVO.getMax());
			} 
			if("Y".equals(bankVO.getUse_min()) && Double.parseDouble(draw.getFee()) < Double.parseDouble(bankVO.getMin())){
				dao.updateDrawFee(drawVO.getId(), bankVO.getMin());
			}
			draw =  dao.getLogCashDrawById(drawVO.getId());
			//提现日志生成
			dao.insertLogDrawInter(draw);
			//提现订单关联日志
			dao.updateDrawConn(draw);
		}
		return re;
	}

	public List<LogCashDrawVO> selectWithdrawDepositLog(String member_id, LogCashDrawVO logCashDrawVO) {
		return dao.selectWithdrawDepositLog(member_id,logCashDrawVO);
	}
	public int selectMemberById(String logcashdraw_id,String member_id) {
		return dao.selectMemberById(logcashdraw_id,member_id);
	}

	public int getTotal(LogBuyCashVO logBuyCashVO) {
		return dao.getTotal(logBuyCashVO);
	}

	public List<LogBuyCashVO> selectLogBuyCashList(LogBuyCashVO logBuyCashVO) {
		return dao.selectLogBuyCashList(logBuyCashVO);
	}


	public int checkLogBuyCashById(String logByCashId) {
		return dao.checkLogBuyCashById(logByCashId);
	}

	public LogBuyCashVO selectLogBuyCashOne(String logByCashId, String member_id) {
		
		LogBuyCashVO vo =new LogBuyCashVO();
		int count1 = dao.getLogBuyCashOneCount1(logByCashId,member_id);
		int count2 = dao.getLogBuyCashOneCount2(logByCashId,member_id);
		if(count1>=2){
			 vo=dao.selectLogBuyCashOne1(logByCashId,member_id);
		}else if(count2>=2){
			 vo=dao.selectLogBuyCashOne2(logByCashId,member_id);
		}else{
			 vo=dao.selectLogBuyCashOne(logByCashId,member_id);
		}
		return vo;
	}

	public int getUnSettlementTotal(String member_id, String lang) {
		
		return dao.getUnSettlementTotal(member_id,lang);
	}

	public List<LogEarnVO> selectUnSettlementList(String member_id, LogEarnVO logEarnVO, String lang) {
		return dao.selectUnSettlementList(member_id,logEarnVO,lang);
	}

	public int checkUnSettlementOneById(String unSettlement_id) {
		return dao.checkUnSettlementOneById(unSettlement_id);	
	}

	public LogEarnVO selectUnSettlementOne(String unSettlement_id, String member_id) {
		
		LogEarnVO vo =new LogEarnVO();
		int count = dao.getUnSettlementOneByIdCount(unSettlement_id,member_id);
		if(count>=2){
			 vo=dao.selectUnSettlementOne1(unSettlement_id,member_id);
		}else{
			 vo=dao.selectUnSettlementOne(unSettlement_id,member_id);
		}
		return vo;
	}

	public int checkIncomeAndExpensesOne(String member_id, String incomeAndExpenses_id) {
		return dao.checkIncomeAndExpensesOne(member_id,incomeAndExpenses_id);
	}

	public LogEarnVO selectIncomeAndExpensesOne(String member_id, String incomeAndExpenses_id) {
		LogEarnVO vo =new LogEarnVO();
		int count1 = dao.getIncomeAndExpensesOneCount1(member_id,incomeAndExpenses_id);
		int count2 = dao.getIncomeAndExpensesOneCount2(member_id,incomeAndExpenses_id);
		if(count1>=2){
			 vo=dao.selectIncomeAndExpensesOne1(member_id,incomeAndExpenses_id);
		}else if(count2>=2){
			 vo=dao.selectIncomeAndExpensesOne2(member_id,incomeAndExpenses_id);
		}else{
			 vo=dao.selectIncomeAndExpensesOne(member_id,incomeAndExpenses_id);
		}
		return vo;
	}

	public int getIncomeAndExpensesTotal(LogEarnVO logEarnVO) {
		return dao.getIncomeAndExpensesTotal(logEarnVO);
	}

	public List<LogEarnVO> selectIncomeAndExpensesList(LogEarnVO logEarnVO) {
		return dao.selectIncomeAndExpensesList(logEarnVO);
	}
	public int getWithdrawDepositTotal(String member_id, String lang) {
		return dao.getWithdrawDepositTotal(member_id,lang);
	}

	public List<LogEarnVO> selectWithdrawDepositLog(String member_id, LogEarnVO logEarnVO, String lang) {
		return dao.selectWithdrawDepositLog(member_id,logEarnVO,lang);
	}

	public LogEarnVO selectWithdrawDepositLogOne(String member_id, String withdrawDeposit_id) {
		return dao.selectWithdrawDepositLogOne(member_id,withdrawDeposit_id);
	}
	
	public String getRechargeNoById(String id) {
		return dao.getRechargeNoById( id);
	}
	
	public CashRechargeVO getRechargeByNo(String out_trade_no) {
		return dao.getRechargeByNo(out_trade_no);
	}
	
	public void updateRechargeSuccessAndAddCash(OrderPayVO payVO) {
		//增加用户余额
		int num = dao.addMemberFreeCash(payVO);
		if(num > 0){
			//添加充值信息 order_id变为订单ID
			dao.insertMoneyRecharge(payVO);
			//修改支付单 状态
			dao.updateOrderPayState(payVO);
			//添加入账日志 id变为日志ID
			dao.insertCashInLog1(payVO);
			//关联充值到日志
			dao.updateRechargeConnLog(payVO);
			//余额充值推送 start
			MemberVO membervo=memberDao.getAllInfoById(payVO.getMember_id());
			pushservice.pushBalanceRecharge(membervo,payVO);
			//end
		}
	}
	
	public void updateRechargeSuccessAndAddCashEn(OrderPayVO payVO) {
		//增加用户余额
		int num = dao.addMemberFreeCashEn(payVO);
		if(num > 0){
			//添加充值信息 order_id变为订单ID
			dao.insertMoneyRecharge(payVO);
			//修改支付单 状态
			dao.updateOrderPayState(payVO);
			//添加入账日志 id变为日志ID
			dao.insertCashInLog1(payVO);
			//关联充值到日志
			dao.updateRechargeConnLog(payVO);
			//余额充值推送 start
			MemberVO membervo=memberDao.getAllInfoById(payVO.getMember_id());
			pushinterservice.pushBalanceRecharge(membervo,payVO);
			//end
		}
	}
	
	public void deleteRechargeOrderById(String id) {
		dao.deleteRechargeOrderById(id);
	}

	public int updateNoReadMessages(List<LogEarnVO> myAssetsList) {
		return dao.updateNoReadMessages(myAssetsList);
	}

	public int getMyAssetsTotalById(LogEarnVO logEarnVO) {
		return dao.getMyAssetsTotalById(logEarnVO);
	}
	public List<LogEarnVO> selectAllMessageById(LogEarnVO logEarnVO) {
		
		String host = configDao.loadConfig("HOST");
		String world_uniposts_url = configDao.loadConfig("WORLD_UNIPOSTS_URL");
		String version = configDao.loadConfig("VERSION");
		String ticket_name = configDao.loadConfig("TICKET_NAME");
		
		List<LogEarnVO> myAssetsList =dao.selectAllMessageById(logEarnVO);
		ArrayList<LogEarnVO> places = new ArrayList<LogEarnVO>();
		DecimalFormat    df   = new DecimalFormat("######0.00");
		double num_i = 0; 
		for(int i=0; i<myAssetsList.size(); i++){
			myAssetsList.get(i).setShow_yn("0");
			myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getFree_cash());
			if(logEarnVO.getLang()==null){
				if("1".equals(myAssetsList.get(i).getState())){
					myAssetsList.get(i).setUrl(host+version+"/money/unSettlement");
				}
				if("2".equals(myAssetsList.get(i).getState())){
					myAssetsList.get(i).setUrl(host+version+"/money/incomeAndExpensesLog");
				}
				if("1".equals(myAssetsList.get(i).getType())){
					if(myAssetsList.get(i).getPay_type()==null){
						myAssetsList.get(i).setType_name("离线充值");
					}
					if(myAssetsList.get(i).getPay_type()!=null){
						myAssetsList.get(i).setType_name("余额充值");
					}
				}
				if("2".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("奖项获得");
				}
				if("3".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("退款 ");
				}
				if("4".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("余额提现");
				}
				if("5".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("购物");
				}
				if("6".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name(ticket_name+"充值");
				}
				if("7".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("会员升级");
				}
				
			}else{
				if("1".equals(myAssetsList.get(i).getState())){
					myAssetsList.get(i).setUrl(world_uniposts_url+"/#/account_waitCash");
				}
				if("2".equals(myAssetsList.get(i).getState())){
					myAssetsList.get(i).setUrl(host+version+"/#/account_cash");
				}
				if("1".equals(myAssetsList.get(i).getType())){
					if(myAssetsList.get(i).getPay_type()==null){
						myAssetsList.get(i).setType_name("System Top up");
					}
					if(myAssetsList.get(i).getPay_type()!=null){
						myAssetsList.get(i).setType_name("Account Top up");
					}
				}
				if("2".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("Rewards");
				}
				if("3".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("Refund ");
				}
				if("4".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("Withdraw");
				}
				if("5".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("Purchase");
				}
				if("6".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("Uni ticket Top up");
				}
				if("7".equals(myAssetsList.get(i).getType())){
					myAssetsList.get(i).setType_name("Level-Up");
				}
				//零售奖
				if("1".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("Sharing Reward");
				}
				//活力奖
				if("2".equals(myAssetsList.get(i).getAward())){
					if(myAssetsList.get(i).getAward().indexOf("+")==-1){
						myAssetsList.get(i).setAward_name("Sharing Reward");
					}else{
						myAssetsList.get(i).setAward_name("Sharing Reward+Sale Bonus");
					}
				}
				//助力奖
				if("3".equals(myAssetsList.get(i).getAward())){
					if(myAssetsList.get(i).getAward().indexOf("+")==-1){
						myAssetsList.get(i).setAward_name("Sale Bonus");
					}else{
						myAssetsList.get(i).setAward_name("Sharing Reward+Sale Bonus");
					}
				}
				//黑卡奖
				if("4".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("Black Card Reward");
				}
				//补贴奖
				if("5".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("Additional Reward");
				}
				//区域代理奖
				if("6".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("Regional Agent Reward");
				}
				//市代理奖
				if("7".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("City Agent Reward");
				}
				//合伙人奖
				if("8".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("Partnership Reward");
				}
				//直推奖
				if("9".equals(myAssetsList.get(i).getAward())){
					myAssetsList.get(i).setAward_name("Referral Bonus");
				}
			}
			//
			if("2".equals(myAssetsList.get(i).getAward())){
				myAssetsList.get(i).setShow_yn("1");
				myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getEarn());
			}
			if("3".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setShow_yn("2");
			}
			if("4".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setShow_yn("2");
			}
			if("3".equals(myAssetsList.get(i).getType())){
				if(!"0.00".endsWith(myAssetsList.get(i).getFree_cash())){
					places.add(myAssetsList.get(i));
				}
			}else{
				places.add(myAssetsList.get(i));
			}
		}
		
		
		return places;
	}

	public String getawardName(String award_id) {
		return awardDao.getAwardName(award_id);
	}
	@Override
	public List<LogEarnVO> selectRechargeTicketAwardList(String memberId, String connNo) {
		List<LogEarnVO> myAssetsList =dao.selectRechargeTicketAwardList(memberId,connNo);
		DecimalFormat    df   = new DecimalFormat("######0.00");
		double num_i = 0; 
		for(int i=0; i<myAssetsList.size(); i++){
			myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getFree_cash());
			
			if(("2".equals(myAssetsList.get(i).getAward())) || (myAssetsList.get(i).getAward().indexOf("3,2")>-1) ||(myAssetsList.get(i).getAward().indexOf("2,3")>-1) ){
				myAssetsList.get(i).setShow_yn("1");
				myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getEarn());
			}
			if("3".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setShow_yn("2");
			}
			if("4".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setShow_yn("2");
			}
		}
		return myAssetsList;
	}
	@Override
	public List<LogEarnVO> selectBuyGoodsProfitAwardList(String memberId, String order_id) {
		List<LogEarnVO> myAssetsList =dao.selectBuyGoodsProfitAwardList(memberId,order_id);
		DecimalFormat    df   = new DecimalFormat("######0.00");
		double num_i = 0; 
		for(int i=0; i<myAssetsList.size(); i++){
			myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getFree_cash());
			
			if(("2".equals(myAssetsList.get(i).getAward())) || (myAssetsList.get(i).getAward().indexOf("3,2")>-1) ||(myAssetsList.get(i).getAward().indexOf("2,3")>-1) ){
				myAssetsList.get(i).setShow_yn("1");
				myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getEarn());
			}
			if("3".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setShow_yn("2");
			}
			if("4".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setShow_yn("2");
			}
		}
		return myAssetsList;
	}
	@Override
	public List<LogEarnVO> selectBuyGoodsPackageProfitAwardList(String memberId, String order_id) {
		List<LogEarnVO> myAssetsList =dao.selectBuyGoodsPackageProfitAwardList(memberId,order_id);
		DecimalFormat    df   = new DecimalFormat("######0.00");
		double num_i = 0; 
		for(int i=0; i<myAssetsList.size(); i++){
			myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getFree_cash());
			
			if(("2".equals(myAssetsList.get(i).getAward())) || (myAssetsList.get(i).getAward().indexOf("3,2")>-1) ||(myAssetsList.get(i).getAward().indexOf("2,3")>-1) ){
				myAssetsList.get(i).setShow_yn("1");
				myAssetsList.get(i).setEarn_money(myAssetsList.get(i).getEarn());
			}
			if("3".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setShow_yn("2");
			}
			if("4".equals(myAssetsList.get(i).getAward())){
				num_i=Double.valueOf(myAssetsList.get(i).getFree_cash()).doubleValue()+Double.valueOf(myAssetsList.get(i).getBuy_cash()).doubleValue();
				myAssetsList.get(i).setEarn_money(df.format(num_i)+"");
				myAssetsList.get(i).setProfit_cash(myAssetsList.get(i).getFree_cash());
				myAssetsList.get(i).setShow_yn("2");
			}
		}
		return myAssetsList;
	}
	@Override
	public LogEarnVO packPaginationInfo(LogEarnVO logEarnVO) {
		LogEarnVO pagination = new LogEarnVO();
		pagination.setPage_index(logEarnVO.getPage_index());
		pagination.setPage_size(logEarnVO.getPage_size());
		pagination.setPage_count(logEarnVO.getPage_count());
		pagination.setTotal(logEarnVO.getTotal());
		return pagination;
	}
	@Override
	public LogBuyCashVO packPaginationInfo(LogBuyCashVO logBuyCashVO) {
		LogBuyCashVO pagination = new LogBuyCashVO();
		pagination.setPage_index(logBuyCashVO.getPage_index());
		pagination.setPage_size(logBuyCashVO.getPage_size());
		pagination.setPage_count(logBuyCashVO.getPage_count());
		pagination.setTotal(logBuyCashVO.getTotal());
		return pagination;
	}
	@Override
	public String getIncomeSumMoney(String member_id) {
		// TODO Auto-generated method stub
		return dao.getIncomeSumMoney(member_id);
	}
	@Override
	public String getUnSettledMoneyEn(String member_id) {
		return dao.getUnSettledMoneyEn(member_id);
	}
	@Override
	public BankVO getDrawBank(String bank_id) {
		return dao.getDrawBank(bank_id);
	}
	@Override
	public String getRateCash(String id) {
		return dao.getRateCash(id);
	}
}
